
package com.crowdfunding.circle.analyze.service;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crowdfunding.circle.analyze.dao.ProjectTypeDao;
import com.crowdfunding.circle.analyze.dao.TypeDetailDao;
import com.crowdfunding.circle.analyze.entity.ProjectTypeVO;
import com.crowdfunding.circle.analyze.entity.TypeDetailVO;
import com.crowdfunding.circle.analyze.param.ProjectTypeParam;

/**
 * 项目类型对应的拆解项Service实现类
 *
 * @author 朱国军
 * @version 2016年07月03日 朱国军
 */
@Service
public class ProjectTypeService {

    @Autowired
    private ProjectTypeDao projectTypeDao;

    @Autowired
    private TypeDetailDao typeDetailDao;

    /**
     * 修改
     *
     * @param entity 要修改的项目类型对应的拆解项对象
     * @return 返回修改的条数
     */
    public Integer update(ProjectTypeVO entity, String userId) {
        String projectTypeId = entity.getProjectTypeId();
        if (StringUtils.isBlank(projectTypeId)) {
            entity.setDataStatus(0);
            entity.setPublishUser(userId);
            projectTypeId = UUID.randomUUID().toString();
        }
        List<TypeDetailVO> typeDetailList = typeDetailDao.queryListByTypeId(projectTypeId);
        
        String[] typeDetailArray = StringUtils.split(entity.getTypeDetail(), "#");
        
        for (int i = 1; i <= typeDetailArray.length; i++) {
            if (typeDetailList != null && typeDetailList.size() >= i) {
                typeDetailList.get(i - 1).setTypeDetailId(typeDetailArray[i - 1]);
            } else {
                TypeDetailVO typeDetail = new TypeDetailVO();
                typeDetail.setAllowCharge(1);
                typeDetail.setDataStatus(0);
                typeDetail.setProjectTypeId(projectTypeId);
                typeDetail.setPublishUser(userId);
                typeDetail.setTypeDetailId(UUID.randomUUID().toString());
                typeDetail.setTypeDetailIndex(i);
                typeDetail.setTypeDetaiName(typeDetailArray[i - 1]);
                typeDetailList.add(typeDetail);
            }
        }
        if (typeDetailList != null && typeDetailList.size() > typeDetailArray.length) {
            for (int i = typeDetailArray.length; i < typeDetailList.size(); i++) {
                typeDetailList.remove(i);
            }
        }
        typeDetailDao.delete(projectTypeId);
        
        for (TypeDetailVO typeDetailVO : typeDetailList) {
            typeDetailDao.insert(typeDetailVO);
        }
        
        Integer afterCount = 0;
        
        if (StringUtils.isBlank(entity.getProjectTypeId())) {
            entity.setProjectTypeId(projectTypeId);
            afterCount = projectTypeDao.insert(entity);
        } else {
            afterCount = projectTypeDao.update(entity);
        }
        
        return afterCount;
    }

    /**
     * 根据ID获取项目类型对应的拆解项实体
     *
     * @param id 主键
     * @return 项目类型对应的拆解项信息
     */
    public ProjectTypeVO findById(String id) {
        ProjectTypeVO projectTypeVO = projectTypeDao.findById(id);

        List<TypeDetailVO> typeDetailList = typeDetailDao.queryListByTypeId(id);

        String typeDetail = "";

        for (TypeDetailVO typeDetailVO : typeDetailList) {
            typeDetail = typeDetail + typeDetailVO.getTypeDetaiName() + "#";
        }

        if (StringUtils.endsWith(typeDetail, "#")) {
            typeDetail = StringUtils.substring(typeDetail, 0, typeDetail.length() - 1);
        }
        projectTypeVO.setTypeDetail(typeDetail);

        return projectTypeVO;
    }

    /**
     * 按查询条件【分页】查询
     *
     * @param param 项目类型对应的拆解项查询条件
     * @return 项目类型对应的拆解项列表
     */
    public List<ProjectTypeVO> queryByPage(ProjectTypeParam param) {
        return projectTypeDao.queryByPage(param);
    }

    /**
     * 根据ID删除项目类型对应的拆解项
     *
     * @param id 主键
     */
    public Integer delete(String id) {
        return projectTypeDao.delete(id);
    }

    /**
     * 查询全部记录
     */
    public List<ProjectTypeVO> queryAll() {
        ProjectTypeParam param = new ProjectTypeParam();
        param.setPageSize(Integer.MAX_VALUE);
        return projectTypeDao.queryByPage(param);
    }
}
